#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import requests
import random

"""
    @author: yubo.eth

    @desc: 指标计算工具类
    单独使用请执行 
    pip3 install requests
"""
class MessageUtils(object):


    """
        discord 发送消息
        
        chanel_list:发送群组
        authorization:discord中权限验证码
        context:发送内容
    """
    def send_msg_discord(chanel_list,authorization,context):
        header = {
            "Authorization": authorization,
            "Content-Type": "application/json",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36",
        }
        msg = {
            "content": context,
            "nonce": "82329451214{}33232234".format(random.randrange(0, 1000)),
            "tts": False,
        }
        url = "https://discord.com/api/v9/channels/{}/messages".format(chanel_list)
        try:
            res = requests.post(url=url, headers=header, data=json.dumps(msg))
            print(res.content)
        except:
            pass


    """
        dingding 发送消息
        https://oapi.dingtalk.com/robot/send?access_token=50ee90b7049687f43ee0aa35f48f1841648ee2ff7d3f1391d6d6c9808869cf08
    """
    def send_msg_dingtalk(url, content, mobile_list):
        headers = {'Content-Type': 'application/json'}
        data = {
            "msgtype": "text",
            "text": {
                # 要发送的内容【支持markdown】【！注意：content内容要包含机器人自定义关键字，不然消息不会发送出去，这个案例中是test字段】
                "content": content
            },
            "at":{
            # 要@的人
            "atMobiles": mobile_list,
            # 是否@所有人
            "isAtAll": False
            }
        }
        r = requests.post(url, headers=headers, data=json.dumps(data))
        print(r.text)


